package com.jzqf.harmonydemo.ability.slice;

import com.jzqf.harmonydemo.ResourceTable;
import com.jzqf.harmonydemo.library.camera.CameraManager;
import com.jzqf.harmonydemo.library.camera.SurfaceProviderPreview;
import com.jzqf.harmonydemo.utils.LogUtil;
import com.jzqf.harmonydemo.utils.file.FileUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.Element;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.app.Environment;
import ohos.media.image.Image;
import ohos.media.image.ImageReceiver;
import ohos.media.image.common.ImageFormat;

/**
 * 相机预览
 *
 * @author LiuWeiHao
 * 2021/5/21 15:44
 */
public class CameraAbilitySlice extends AbilitySlice {
    private final String TAG = CameraAbilitySlice.class.getSimpleName();
    private CameraManager mCameraManager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_camera);
        initCamera();
//        initView();
    }

    private void initView() {
        //TODO 组件无法在预览控件上层显示
        Button takePhotoBtn = new Button(this);
        //takePhotoBtn.setBackground(Element.DEFAULT_BOUNDS_RECT);
        DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(100, 100);
        takePhotoBtn.setLayoutConfig(params);
        Component rootLayout = findComponentById(ResourceTable.Id_root_layout);
        if(rootLayout instanceof ComponentContainer){
            ((ComponentContainer) rootLayout).addComponent(takePhotoBtn);
        }
        takePhotoBtn.setClickedListener(component -> {
        });
    }

    private void initCamera() {
        SurfaceProviderPreview surfaceProvider = (SurfaceProviderPreview) findComponentById(ResourceTable.Id_surface_preview);
        mCameraManager = new CameraManager(this);
        surfaceProvider.pinToZTop(true);
        mCameraManager.setImageArrivalListener(imageArrivalListener);
        mCameraManager.init(surfaceProvider);
    }

    private final ImageReceiver.IImageArrivalListener imageArrivalListener = imageReceiver -> {
        //最后一帧图像
        Image image = imageReceiver.readLatestImage();
        //TODO 没有图像数据输出
        LogUtil.d(TAG, "imageArrivalListener Thread=" + Thread.currentThread().getName());
        save(image);
        image.release();//释放
    };

    private void save(Image image) {
        Image.Component imageComponent = image.getComponent(ImageFormat.ComponentType.JPEG);
        byte[] bytes = new byte[imageComponent.remaining()];
        imageComponent.read(bytes);
        String path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath() + "/" + System.currentTimeMillis() + ".jpg";
        FileUtil.saveFile(bytes, path);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mCameraManager.release();
    }
}
